package db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public class DBConnectionHelper {
    
    static Logger logger = Logger.getLogger(DBConnectionHelper.class);

    public static Connection getConnection() {
	try {
	    Class.forName("org.postgresql.Driver");
	} catch (ClassNotFoundException e) {
	    logger.error("Postgresql driver not found!");

	}	

	Connection connection = null;
	try {
	    connection = DriverManager.getConnection(
		    "jdbc:postgresql://imbi.ld.ttu.ee:5432/dog", "t103769", "Le6JnRoR");

	} catch (SQLException e) {
	    logger.error("Connection Failed! Check output console");
	}
	return connection;
    }
    
    public static ResultSet executeQuery(String sql){
	Connection con = getConnection();
	try {
	Statement st = con.createStatement();
	    ResultSet res = st.executeQuery(sql);
	    return res;
	} catch (SQLException e) {
	   return null;
	}finally{
	    try {
		con.close();
	    } catch (SQLException e) {
		logger.error("Closing connection failed!");
	    }
	}
	
    }    
    
}
